'use strict'

$(function () {
    let id = location.search.split('?')[1]
    console.log(id);
    if (id) {
        $.get(`http://localhost:8000/blog`, data => {
            let newData = data.filter(item => {
                return item.id == id
            })
            $('[name=id]').val(newData[0].id)
            $('[name=tite]').val(newData[0].tite)
            $('[name=imports]').val(newData[0].imports)
            $('[name=text]').val(newData[0].text)
            $('[name=cat]').val(newData[0].cat)
            $('[name=atou]').val(newData[0].atou)
            $('[name=time]').val(newData[0].time)
        })
    }
})

function save() {
    let obj = {
        id: $('[name=id]').val(),
        tite: $('[name=tite]').val(),
        imports: $('[name=imports]').val(),
        text: $('[name=text]').val(),
        cat: $('[name=cat]').val(),
        atou: $('[name=atou]').val(),
        time: $('[name=time]').val()
    }
    if (obj.id) {
        putBlog(obj.id, obj).then(data => {
            if (data.code === 1000) {
                console.log(333333);
                console.log(obj);
                location.href = './index.html'
            }
        })
    } else {
        console.log(22222);
        postBlog(obj).then(data => {
            if (data.code === 1000) {
                location.href = './index.html'
            }
        })
    }
}

function cancel() {
    location.href = './index.html'
}